/**
 * 
 */
package com.roc.web.dwr;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;

import com.roc.dao.DaoException;
import com.roc.dao.IMerchantDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.service.IPagenitionService;
import com.roc.util.Constance;
import com.roc.util.Utils;
import com.roc.util.constant.Constant;

/**
 * @author Administrator
 */
@RemoteProxy
public class AddressQuery {
	static Logger logger = LogManager.getLogger(DwrTest.class);

	@Resource(name = "sqlBaseDao")
	private ISQLBaseDao sqlBaseDao;
	
	@Autowired
	IPagenitionService pagenitionService;
	
	@Autowired
	private IMerchantDao merchantDao;
	
	//load pro
	@RemoteMethod
	public List<Map<String, Object>> loadProvices() {
		logger.debug("-----加载省份信息----");
		List<Map<String, Object>> queryResult = null;
		String sql = "SELECT DIC_CODE as \"code\",NAME as \"name\"  FROM TBL_DICTIONARYLAYERITEM T WHERE T.DIC_TYPE = 'GB_Area_MAP' AND T.DIC_LEVEL = '0' ORDER BY DISP_ORDER ASC";
		try {
			queryResult = sqlBaseDao.querySqlToMap(sql);
		} catch (DaoException e) {
			logger.debug("加载省份信息失败",e);
		}
		return queryResult;
	}
	//load   city 
	@RemoteMethod
	public List<Map<String, Object>> loadCitys(String provice) {
		logger.debug("----加载城市信息------省份代码[" + provice+"]--------");
		List<Map<String, Object>> queryResult = null;

		try {
			long pCode = Long.valueOf(provice);
			String sql = "SELECT DIC_CODE as \"code\",NAME as \"name\"  FROM TBL_DICTIONARYLAYERITEM T WHERE t.dic_code<>'370100' AND T.DIC_TYPE = 'GB_Area_MAP' AND T.DIC_LEVEL = '1' AND DIC_PCODE="+pCode+" ORDER BY DISP_ORDER ASC";
			queryResult = sqlBaseDao.querySqlToMap(sql);
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+provice,nfe);
		} catch (DaoException e) {
			logger.debug("--加载城市信息失败省份代码[" + provice+"]--",e);
		}
		return queryResult;
	}
	
	@RemoteMethod
	public List<Map<String, Object>> loadAreas(String city) {
		logger.debug("----加载区县信息------城市代码[" + city+"]--------" );
		List<Map<String, Object>> queryResult = null;

		try {
			long cCode = Long.valueOf(city);
			String sql = "SELECT DIC_CODE as \"code\",NAME as \"name\"  FROM TBL_DICTIONARYLAYERITEM T WHERE T.DIC_TYPE = 'GB_Area_MAP' AND T.DIC_LEVEL = '2' AND DIC_PCODE="+cCode+" ORDER BY DISP_ORDER ASC";
			queryResult = sqlBaseDao.querySqlToMap(sql);
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+city,nfe);
		} catch (DaoException e) {
			logger.debug("加载区县信息失败城市代码[" + city+"]",e);
		}
		return queryResult;
	}
	
	
	
	
	//load　PayBank
	@RemoteMethod
	public List<Map<String, Object>> loadPayBank() {
		logger.debug("dwr loadPayBank " );
		List<Map<String, Object>> queryResult = null;

		try {
			
			String sql = "SELECT c.BANKID as \"code\",c. bankname as \"name\" FROM t_bank_info C  ";
			queryResult = sqlBaseDao.querySqlToMap(sql.toUpperCase());
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:",nfe);
		} catch (DaoException e) {
			logger.debug("加载信息失败",e);
		}
		return queryResult;
	}
	//load  paybankbranchName
	@RemoteMethod
	public List<Map<String, Object>> loadPayBankBranch(String payBank,String proid,String cityid) {
		logger.debug("dwr loadPayBank " + payBank);
		List<Map<String, Object>> queryResult = null;

		try {
			long pCode = Long.valueOf(payBank);
			String sql = "SELECT c.BANKBRANCHID as \"code\",c.BANKBRANCHNAME as \"name\" FROM paybankinfo c where c.bankid="+pCode+" and c.provinceid='"+proid+"' AND c.cityid='"+cityid+"' ";
			queryResult = sqlBaseDao.querySqlToMap(sql.toUpperCase());
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+payBank,nfe);
		} catch (DaoException e) {
			logger.debug("加载信息失败",e);
		}
		return queryResult;
	}
	
	
	
	@RemoteMethod
	public List<Map<String, Object>> loadSubMerchant(String merLevel) {
		logger.debug("dwr loadSubMerchant " + merLevel);
		List<Map<String, Object>> queryResult = null;

		try {
			String sql = "SELECT m.id as \"code\",a.real__name as \"name\" FROM t_merchant m WHERE m.mer_level='"+merLevel+"'";
			queryResult = sqlBaseDao.querySqlToMap(sql.toUpperCase());
		} catch (NumberFormatException nfe) {
			logger.debug("商户级别:"+merLevel,nfe);
		} catch (DaoException e) {
			logger.debug("加载信息失败",e);
		}
		return queryResult;
	}
	
	@RemoteMethod//查询机构对应的代理商
	public List<Map<String, Object>> merInfoList(String orgCode,String level,String merchantId,String merType) {
		logger.debug("dwr loadStreet " + orgCode);
		List<Map<String, Object>> queryResult = null;
		//String sql2="";//"SELECT t.id as \"id\",t.real_name as \"name\" FROM t_merchant t start WITH id ="+merchantId+" and t.mer_type = '01' connect BY prior t.id = t.superior order by t.real_name ";
		
        String sql = "";
		try {
			//long cCode = Long.valueOf(orgCode);
				if(!level.equals("0")){
					if(Constance.MERCHANT_TYPE_SYSTEM.equals(merType)||Constance.MERCHANT_TYPE_MARKET.equals(merType)){
						sql = "select m.id as \"id\",m.real_name as \"name\" from t_merchant m where m.mer_level = "+level+" and m.org_Code ='"+orgCode+"'  and m.mer_type = '01'  ";	
					}else if(Constance.MERCHANT_TYPE_CAIWU.equals(merType)||Constance.MERCHANT_TYPE_STORE.equals(merType)){
						sql = "select m.id as \"id\",m.real_name as \"name\" from t_merchant m where m.mer_level = "+level+" and m.org_Code ='"+orgCode+"' and m.superior=1  and m.mer_type = '01'  ";	
						
					}else if(Constance.MERCHANT_TYPE_RECOMANFER.equals(merType)){
						sql = "select m.id as \"id\",m.real_name as \"name\" from t_merchant m where m.mer_level = "+level+" and   m.recommender_id="+merchantId+"  and m.mer_type = '01'  ";
					}else{
						sql="SELECT t.id as \"id\",t.real_name as \"name\" FROM t_merchant t start WITH id ="+merchantId+" and t.mer_type = '01' and t.mer_level='"+level+"' and t.org_code='"+orgCode+"' connect BY prior t.id = t.superior order by t.real_name ";
					}
				}else{
					
					if(Constance.MERCHANT_TYPE_SYSTEM.equals(merType)||Constance.MERCHANT_TYPE_MARKET.equals(merType)){
						sql = "select m.id as \"id\",m.real_name as \"name\" from t_merchant m where m.org_Code ='"+orgCode+"'  and m.mer_type = '01'  ";
					}else if(Constance.MERCHANT_TYPE_CAIWU.equals(merType)||Constance.MERCHANT_TYPE_STORE.equals(merType)){
						sql = "select m.id as \"id\",m.real_name as \"name\" from t_merchant m where m.org_Code ='"+orgCode+"'  and m.superior=1  and m.mer_type = '01'  ";
					}else if(Constance.MERCHANT_TYPE_RECOMANFER.equals(merType)){
						sql = "select m.id as \"id\",m.real_name as \"name\" from t_merchant m where m.recommender_id="+merchantId+"  and m.mer_type = '01'  ";
					}else{
						sql="SELECT t.id as \"id\",t.real_name as \"name\" FROM t_merchant t start WITH id ="+merchantId+" and t.mer_type = '01'  and t.org_code='"+orgCode+"' connect BY prior t.id = t.superior order by t.real_name ";
					}
				}
				
	
			
			queryResult = sqlBaseDao.querySqlToMap(sql.toUpperCase());
		}catch (DaoException e) {
			logger.debug("加载信息失败",e);
		}
		return queryResult;
	}
	
	
	@RemoteMethod
	public String checkBusLicences(String busLicences) {
		logger.debug("dwr checkBusLicences in " );
        String backFlag = "false";
        int count = 0;
        
        String isCheckBuss =  Utils.getConfigValue("isCheckBusinessLicences");
        if(!Constant.IS_CHECK_MER_BUSS.equals(isCheckBuss)){
        	return backFlag;
        }
		try {
			
			count = merchantDao.checkBusLicencesTimes(busLicences);
			
			if(count>=Constant.MER_BUSS_TIMES){
				backFlag = "true";	
			}
			
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+busLicences,nfe);
		} catch (DaoException e) {
			logger.debug("查询营业执照号失败:"+busLicences,e);
		}
		return backFlag;
	}
	
	
	@RemoteMethod
	public String checkCardNo(String cardNo) {
		logger.debug("dwr checkCardNo in " );
        String backFlag = "false";
        int count = 0;
		try {
			count = merchantDao.checkCardNoTimes(cardNo);
			
			if(count>=Constant.MER_CARD_TIMES){
				backFlag = "true";	
			}
			
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+cardNo,nfe);
		} catch (DaoException e) {
			logger.debug("查询证件号码失败cardNo:"+cardNo,e);
		}
		return backFlag;
	}
	
	
	//判断该代理商是否已经录单
	@RemoteMethod
	public String checkIsAddMer(Long agentId) {
		logger.debug("dwr checkIsAddMer in " );
        String backFlag = "false";
        String count = "";
        String sql = " SELECT count(*) count from ESMIDINFO_tmp_merinfo where agent_id = "+agentId+" ";
		try {
			
			List<Object> list = sqlBaseDao.queryAll(sql);
			if(list!=null && list.size()>=1){
				Object o = list.get(0);
				count = o.toString();
			}
			
			if(count ==null || "".equals(count)){
				return backFlag;
			}
			
			if(!"0".equals(count)){
				backFlag = "true";	
			}
			
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+count,nfe);
		} catch (DaoException e) {
			logger.debug("查询用户是否已经录单失败:"+count+"sql:"+sql,e);
		}
		return backFlag;
	}
	
	
	//判断该代理商是否分配序列号
	@RemoteMethod
	public String checkSerialIsBand(Long agentId) {
		logger.debug("dwr checkIsAddMer in " );
        String backFlag = "false";
        String count = "";
        String sql = " SELECT count(*) count from yj_pos_agent where agent_id = "+agentId+" ";
		try {
			
			List<Object> list = sqlBaseDao.queryAll(sql);
			if(list!=null && list.size()>=1){
				Object o = list.get(0);
				count = o.toString();
			}
			
			if(count ==null || "".equals(count)){
				return backFlag;
			}
			
			if(!"0".equals(count)){
				backFlag = "true";	
			}
			
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+count,nfe);
		} catch (DaoException e) {
			logger.debug("查询该代理商是否分配序列号失败:"+count+"sql:"+sql,e);
		}
		return backFlag;
	}
	
	//判断该代理商是否分配序列号,是否新增过特约商户
	@RemoteMethod
	public String checkSerialIsBandAndAddMer(Long agentId) {
		
		String isBand = checkSerialIsBand(agentId);
	
		if("true".equals(isBand)){
			return "true1";
		}
		
		String isAdd = checkIsAddMer(agentId);
		
		if("true".equals(isAdd)){
			return "true2";
		}
		return "false";
		
	}
	
	
	//判断商户号是否存在
	@RemoteMethod
	public String checkMidExist(String mid) {
		logger.debug("dwr checkMidExist in " );
        String backFlag = "false";
        String count = "";
        String sql = " SELECT count(*) count from ESMIDINFO_tmp where mid  = "+mid+" ";
		try {
			
			List<Object> list = sqlBaseDao.queryAll(sql);
			if(list!=null && list.size()>=1){
				Object o = list.get(0);
				count = o.toString();
			}
			
			if(count ==null || "".equals(count)){
				return backFlag;
			}
			
			if(!"0".equals(count)){
				backFlag = "true";	
			}
			
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+count,nfe);
		} catch (DaoException e) {
			logger.debug("查询商户是否使用失败:"+count+"sql:"+sql,e);
		}
		return backFlag;
	}
	
	
	//判断终端号是否存在
	@RemoteMethod
	public String checkTidExist(String tid) {
		logger.debug("dwr checkTidExist in " );
        String backFlag = "false";
        String count = "";
        String sql = " SELECT count(*) count from ESTIDINFO where tid  = "+tid+" ";
		try {
			
			List<Object> list = sqlBaseDao.queryAll(sql);
			if(list!=null && list.size()>=1){
				Object o = list.get(0);
				count = o.toString();
			}
			
			if(count ==null || "".equals(count)){
				return backFlag;
			}
			
			if(!"0".equals(count)){
				backFlag = "true";	
			}
			
		} catch (NumberFormatException nfe) {
			logger.debug("代码非法:"+count,nfe);
		} catch (DaoException e) {
			logger.debug("查询终端号是否使用失败:"+count+"sql:"+sql,e);
		}
		return backFlag;
	}
	
}
